package top.sdwu_top.kbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import top.sdwu_top.kbase.common.aspect.exception.MyException;
import top.sdwu_top.kbase.entity.KbaseAuthRole;
import top.sdwu_top.kbase.entity.KbaseAuthUser;
import top.sdwu_top.kbase.mapper.KbaseAuthRoleMapper;
import top.sdwu_top.kbase.service.IKbaseAuthRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.sdwu_top.kbase.vo.KbaseAuthRoleVo;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@Service
public class KbaseAuthRoleServiceImpl extends ServiceImpl<KbaseAuthRoleMapper, KbaseAuthRole> implements IKbaseAuthRoleService {


    @Override
    public boolean save(KbaseAuthRole entity) {
        if (StringUtils.isBlank(entity.getRoleName())) {
            throw new MyException(50003, "角色名不能为空");
        }
        if (exist(entity)) {
            throw new MyException(50003, "角色名称已存在");
        }
        return super.save(entity);
    }

    @Override
    public boolean delete(String id) {
        if (hasChild(id)) {
            throw new MyException(50003, "该角色含有子角色无法删除");
        }
        return super.removeById(id);
    }

    @Override
    public Page<KbaseAuthRole> page(KbaseAuthRoleVo authRoleVo) {
        QueryWrapper<KbaseAuthRole> qw = Wrappers.query();
        qw.nested(StringUtils.isNotBlank(authRoleVo.getKeyword()), i->{
            i.like("ROLE_NAME", authRoleVo.getKeyword());
        });
        qw.eq(StringUtils.isNotBlank(authRoleVo.getParentId()), "PARENT_ID", authRoleVo.getParentId());
        return super.page(authRoleVo.getPage(), qw);
    }

    @Override
    public List<KbaseAuthRole> getRoleList(KbaseAuthUser user) {
        String userRole = user.getRoleId();
        if (StringUtils.isBlank(userRole)) {
            throw new MyException(50003, "用户角色为空");
        }
        List<KbaseAuthRole> roleList = Lists.newArrayList();
        KbaseAuthRole nowRole = super.getById(userRole);
        if (Objects.isNull(nowRole)) {
            throw new MyException(50003, "用户角色信息为空");
        }
        List<KbaseAuthRole> tempList = Lists.newArrayList(nowRole);
        while(CollectionUtils.isNotEmpty(tempList)) {
            List<KbaseAuthRole> childList = Lists.newArrayList();
            for (KbaseAuthRole kbaseAuthRole : tempList) {
                List<KbaseAuthRole> childrens = this.getChildList(kbaseAuthRole.getId());
                roleList.addAll(childrens);
                childList.addAll(childrens);
            }
            tempList.clear();
            tempList.addAll(childList);
        }
        return roleList;
    }

    private List<KbaseAuthRole> getChildList(String id) {
        QueryWrapper<KbaseAuthRole> qw = Wrappers.query();
        qw.eq("PARENT_ID", id);
        List<KbaseAuthRole> roleList = super.list(qw);
        if (CollectionUtils.isEmpty(roleList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        return roleList;
    }

    @Override
    public boolean updateById(KbaseAuthRole entity) {
        if (exist(entity)) {
            throw new MyException(50003, "该角色名已存在");
        }
        return super.removeById(entity);
    }

    private boolean exist(KbaseAuthRole authRole) {
        QueryWrapper<KbaseAuthRole> qw = Wrappers.query();
        qw.eq("ROLE_NAME", authRole.getRoleName());
        qw.last("limit 1");
        return Objects.nonNull(super.getOne(qw));
    }

    private boolean hasChild(String parentId) {
        QueryWrapper<KbaseAuthRole> qw = Wrappers.query();
        qw.eq("PARENT_ID", parentId);
        return CollectionUtils.isNotEmpty(super.list(qw));
    }
}
